(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.Quickbar 桌面快速启动栏
	 * @singleton
	 */
	HteOS.Quickbar = {

		/**
		 * 初始化快速启动栏
		 */
		init: function() {
			var me = this;
			
			$("<div class=\"hte-quickbar\">" +
					"<div class=\"hte-quickbar-header hte-quickbar-hide\">" + 
						"<span class=\"metro-icon-arrow-left-3 smaller \"></span>" + 
					"</div>" + 
					"<div class=\"hte-quickbar-show\">" + 
						"<span class=\"metro-icon-arrow-right-3 smaller \"></span>" + 
					"</div>" + 
					"<div class=\"hte-quickbar-scroll top\">" + 
							"<span class=\"glyphicon glyphicon-chevron-up\"></span>" + 
				"      </div>" + 
				"      <div class=\"hte-quickbar-scroll bottom\">" + 
				"        <span class=\"glyphicon glyphicon-chevron-down\"></span>" + 
				"      </div>" + 
				"      <div class=\"hte-quickbar-body\">" + 
				"      </div>" + 
				"    </div>").appendTo(HteOS.getContainer());
				
			
			this.initContextMenu();
			this.initEvents();
			
		},
		
		initEvents : function(){
			
			var el = this.getEl(),me = this;

			$(window).resize(function() {
				me.layout();
			});
			
			el.find(".hte-quickbar-scroll.top").click(function() {
				var body = me.getBody();
				var top = body.position().top;
				if (top + 60 > 56) {
					body.animate({
						"top": 56
					}, "fast");
				} else {
					body.animate({
						"top": top + 60
					}, "fast");
				}
			});
			el.find(".hte-quickbar-scroll.bottom").click(function() {
				var body = me.getBody(),el = me.getEl();
				var top = body.position().top;
				var height = body.outerHeight();
				if (0 - (top - 60) > height - el.height()) {
					body.animate({
						"top": 0 - (height - el.height() - 20)
					}, "fast");
				} else {
					body.animate({
						"top": top - 60
					}, "fast");
				}
			});

			el.find(".hte-quickbar-hide").click(function() {
				me.hide()
			});
			el.find(".hte-quickbar-show").click(function() {
				me.show()
			});

			//监听点击事件
			$(document).on('click.hte.quick.data-api', '.hte-quick-item',
				function(event) {
					var app = HteOS.AppManager.getByEl($(this));
					HteOS.TaskManager.start(app);
					me.hide();
					event.stopPropagation();
				});
			
			
			//监听排序事件
			HteOS.EventManager.on("hte.quick.sort", this.sort,this);
			
			HteOS.EventManager.on("hte.apps.instantiated",function(){
				me.render();
				me.dragdrop();
			});
			
			HteOS.EventManager.on("hte.app.pink",function(app){
				me.add(app);
				me.show();
			});
			
			HteOS.EventManager.on("hte.app.unpink",function(app){
				me.remove(app);
			});
		},
		
		render : function(){
			var apps = HteOS.AppManager.apps,array = [];
			for(var id in apps){
				var app = apps[id];
				if(app.quick){
					array.push(app);
				}
			}
			array.sort(function(a,b){
				if(a.quickIndex > b.quickIndex){
					return 1;
				}
				return -1;
			});
			for(var i = 0;i < array.length;i++){
				this.add(array[i]);
			};
			this.layout();
			this.rendered = true;
		},

		/**
		 * 隐藏快速启动栏
		 */
		hide: function() {
			var me = this,
				el = this.getEl();
			/*if ($.is.advance) {
				el.animate({
					left: -80
				}, 'fast', function() {
					el.find(".hte-quickbar-show").animate({
						right: -36
					}, "fast")
				});*/
			//} else {
				el.css("left", -80);
				el.find(".hte-quickbar-show").css("right", -36);
			//}
		},

		/**
		 * 显示快速启动栏
		 */
		show: function() {
			var me = this,
				el = this.getEl();
			/*if ($.is.advance) {
				el.find(".hte-quickbar-show").animate({
					right: 80
				}, 'fast', function() {
					el.animate({
						left: 0
					}, "fast")
				});*/
			//} else {
				el.find(".hte-quickbar-show").css("right", 80);
				el.css("left", 0);
			//}
		},

		/**
		 * 存储快捷启动项
		 *
		 * @property
		 */
		items: {},

		/**
		 * 根据APP实例渲染快捷启动项
		 * @param {HteOS.app.App} app
		 */
		add: function(app) {
			var quick = $("<div class=\"hte-quick-item\"><img src=\"" + app.icon + "\"></div>");
			quick.attr("data-toggle", "tooltip");
			quick.attr("data-placement", "right");
			quick.attr("title", app.name);
			quick.data("appId", app.id);
			quick.attr("id", "hte-quick-" + app.id);
			this.getBody().prepend(quick);
			this.items[app.id] = app;
			if(this.rendered){
				this.layout();
			}
		},

		/**
		 * 根据APP实例移除快捷启动项
		 * @param {HteOS.app.App} app
		 */
		remove: function(app) {
			$("#hte-quick-" + app.id).remove();
			delete this.items[app.id];
			this.layout();
		},

		/**
		 * 根据ID获取快捷启动项
		 * @param {String}  id
		 */
		get: function(id) {
			return this.items[id];
		},

		/**
		 * 调整快捷启动栏的布局
		 */
		layout: function() {
			var body = this.getBody(),
				el = this.getEl();
			if (el.find(".hte-quick-item").length * 60 > el
				.height() - this.getHeader().outerHeight()) {
				body.css("padding-bottom", 35);
				body.css("padding-top", 35);
				el.find(".hte-quickbar-scroll").show();
			} else {
				body.animate({
						"top": 66
					}, "fast");
				body.css("padding-top", 0);
				body.css("padding-bottom", 0);
				el.find(".hte-quickbar-scroll").hide();
			}
		},

		/**
		 * 对快捷方式进行排序
		 */
		sort: function() {
			var me = this,index = 1,
				indexes = "";
			$(".hte-quick-item").each(function(index,el) {
				var appId = $(el).data("appId");
				if(appId){
					if (indexes.length > 0) {
						indexes += ";";
					}
					indexes += appId + "," + index;
				}
				index++;
			});
			if (indexes.length > 0) {
				$.ajax({
					url: HteOS.API.QUICKBAR_LAYOUT_SAVE,
					method: HteOS.API.METHOD,
					data: {
						indexes: indexes
					}
				}).success(function(data) {
					if (!data.success) {
						HteOS.Notification.show("", "应用消息",
							data.message, true)
					}
				}).error(function() {
					HteOS.Notification.show("", "应用消息",
						"保存快速启动布局失败", true)
				});
			}
		},

		getEl: function() {
			return $(".hte-quickbar");
		},
		getBody: function() {
			return $(".hte-quickbar-body");
		},
		getHeader: function() {
			return $(".hte-quickbar-header");
		},
		
		initContextMenu :function(){
			var me = this;
			new HteOS.menu.Menu({
				selector: ".hte-quick-item",
				cls: "hte-tile-contextmenu",
				items: [{
					id: 'open',
					icon: 'glyphicon glyphicon-new-window',
					name: '打开',
					handler: function() {
						var quick = $(".hte-quick-item.active");
						var app = me.items[quick.data("appId")];
						HteOS.TaskManager.start(app);
						me.hide();
					}
				},'-',{
					id :'uninstall',
					icon : 'glyphicon glyphicon-trash',
					name :'移除',
					handler :function(){
						var quick = $(".hte-quick-item.active");
						var app = me.items[quick.data("appId")];
						HteOS.AppManager.unpink(app);
					}
				}],
				events: {
					show: function(target) {
						target = $(target);
						if (!target.hasClass("hte-quick-item")) {
							target = target.parent(".hte-quick-item");
						}
						$(target).addClass("active");
					},
					hide: function() {
						$(".hte-quick-item.active").removeClass("active");
					}
				}
			});
		},
		/**
		 * 启动桌面快捷启动栏拖拽
		 */
		dragdrop: function() {
			$(".hte-quick-item").draggable({
				containment: $(".hte-quickbar-body"),
				revert: false,
				axis: 'y',
				helper : function(){
					var proxy = $(this).clone();
					proxy.removeAttr("data-toggle");
					return proxy;
				},
				start: function() {
					//$(this).css("z-index", 1);
					$(this).css("visibility", "hidden");
					//FIXME 兼容IE8样式
				},
				stop: function() {
					$(this).css("visibility", "visible");
					HteOS.EventManager.trigger("hte.quick.sort");
				}
			});
			$(".hte-quick-item").droppable({
				accept: ".hte-quick-item",
				over: function(event, ui) {
					var target = $(this),
						source = $(ui.draggable.get(0));
					if (source.position().top < target.position().top) {
						source.insertAfter(target);
					} else {
						source.insertBefore(target);
					}
				}/*,
				out: function() {
					$(this).removeClass("tile-dragover");
				},
				drop: function(event, ui) {
					var target = $(this),
						source = $(ui.draggable.get(0));
					if (source.next(".hte-quick-item").get(0) == target.get(0)) {
						source.insertAfter(target);
					} else {
						source.insertBefore(target);
					}
					HteOS.EventManager.trigger("hte.quick.sort");
				}*/
			});
		}
	};
}));